package com.magupe.netty.im.server.session;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;

public class SessionUtil {

	private static final Map<String, Channel> channelMap = new ConcurrentHashMap<>();
	
	public static void bindSession(Session session, Channel channel) {
		channelMap.put(session.getUserNo(), channel);
		channel.attr(Attributes.SESSION).set(session);
	}

    public static void unBindSession(Channel channel) {
        if (hasLogin(channel)) {
        	channelMap.remove(getSession(channel).getUserNo());
            channel.attr(Attributes.SESSION).set(null);
        }
    }
    
    public static boolean hasLogin(Channel channel) {
        return channel.hasAttr(Attributes.SESSION);
    }
    
    public static Session getSession(Channel channel) {
        return channel.attr(Attributes.SESSION).get();
    }

    public static Channel getChannel(String userId) {
        return channelMap.get(userId);
    }
}
